{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tsfresh提取特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tsfresh import select_features\n",
    "from tsfresh import extract_features\n",
    "from tsfresh.feature_extraction import EfficientFCParameters\n",
    "from tsfresh.feature_extraction import ComprehensiveFCParameters\n",
    "from joblib import Parallel, delayed\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "minimal_parameters = {\n",
    "     'length': None,\n",
    "     'abs_energy': None,\n",
    "    \n",
    "    'mean':None,\n",
    "    'mean_abs_change': None,\n",
    "    'mean_change': None,\n",
    "     'variance': None,\n",
    "     'skewness': None,\n",
    "     'kurtosis': None,\n",
    "     'cid_ce': [{'normalize': True}, {'normalize': False}],\n",
    "}\n",
    "\n",
    "settings = minimal_parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_tsfresh_feature_basis(sensor,settings):\n",
    "    '''\n",
    "    描述：\n",
    "        settings，使用tsfresh提取指定特征\n",
    "    参数：\n",
    "        sensor：\n",
    "        settings(dict): 想要提取的特征\n",
    "    '''\n",
    "    if 'vibration_1' in settings.keys():\n",
    "        extracted_features = extract_features(sensor, column_id='id', column_sort='sort_col', n_jobs=12, \n",
    "                                          kind_to_fc_parameters=settings, disable_progressbar = True)\n",
    "    else:\n",
    "        extracted_features = extract_features(sensor, column_id='id', column_sort='sort_col', n_jobs=12, \n",
    "                                          default_fc_parameters=settings, disable_progressbar = False)\n",
    "    return extracted_features\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_tsfresh_feature_parallel(train_no, csv_nos, settings):\n",
    "    '''\n",
    "    描述：\n",
    "        并行提取多个senosr文件的tsfresh特征\n",
    "    参数：\n",
    "        train_no：第几个plc\n",
    "        csv_nos：plc对应的sensor文件个数\n",
    "    '''\n",
    "    \n",
    "    input_dir = './sensors_scale_cur/%s/'%train_no\n",
    "    output_dir = './sensor_tsfresh_minimal/%s/'%train_no\n",
    "    \n",
    "    if not os.path.exists('./sensor_tsfresh_minimal/'):\n",
    "        os.mkdir('./sensor_tsfresh_minimal')\n",
    "    if not os.path.exists('./sensor_tsfresh_minimal/%s'%train_no):\n",
    "        os.mkdir('./sensor_tsfresh_minimal/%s'%train_no)\n",
    "    \n",
    "    def basis_func(idx):\n",
    "        sensor = joblib.load(input_dir + '%d.lz4'%idx)\n",
    "        tmp = gen_tsfresh_feature_basis(sensor, settings)\n",
    "        joblib.dump(tmp, output_dir+'%d.lz4'%idx, compress='lz4')\n",
    "        \n",
    "    Parallel(n_jobs=10,verbose=10)(delayed(basis_func)(i) for i in range(1,csv_nos+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=10)]: Using backend LokyBackend with 10 concurrent workers.\n",
      "[Parallel(n_jobs=10)]: Done   5 tasks      | elapsed:   22.1s\n",
      "[Parallel(n_jobs=10)]: Done  12 tasks      | elapsed:   25.8s\n",
      "[Parallel(n_jobs=10)]: Done  21 tasks      | elapsed:   41.8s\n",
      "[Parallel(n_jobs=10)]: Done  34 out of  48 | elapsed:  1.0min remaining:   24.8s\n",
      "[Parallel(n_jobs=10)]: Done  39 out of  48 | elapsed:  1.1min remaining:   15.2s\n",
      "[Parallel(n_jobs=10)]: Done  44 out of  48 | elapsed:  1.3min remaining:    6.9s\n",
      "[Parallel(n_jobs=10)]: Done  48 out of  48 | elapsed:  1.3min finished\n"
     ]
    }
   ],
   "source": [
    "gen_tsfresh_feature_parallel('01', 48, settings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=10)]: Using backend LokyBackend with 10 concurrent workers.\n",
      "[Parallel(n_jobs=10)]: Done   5 tasks      | elapsed:   21.3s\n",
      "[Parallel(n_jobs=10)]: Done  12 tasks      | elapsed:   39.1s\n",
      "[Parallel(n_jobs=10)]: Done  21 tasks      | elapsed:   43.3s\n",
      "[Parallel(n_jobs=10)]: Done  34 out of  48 | elapsed:  1.0min remaining:   25.4s\n",
      "[Parallel(n_jobs=10)]: Done  39 out of  48 | elapsed:  1.1min remaining:   15.7s\n",
      "[Parallel(n_jobs=10)]: Done  44 out of  48 | elapsed:  1.3min remaining:    6.9s\n",
      "[Parallel(n_jobs=10)]: Done  48 out of  48 | elapsed:  1.3min finished\n"
     ]
    }
   ],
   "source": [
    "gen_tsfresh_feature_parallel('02', 48 , settings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=10)]: Using backend LokyBackend with 10 concurrent workers.\n",
      "[Parallel(n_jobs=10)]: Done   5 tasks      | elapsed:   21.1s\n",
      "[Parallel(n_jobs=10)]: Done  12 tasks      | elapsed:   25.1s\n",
      "[Parallel(n_jobs=10)]: Done  22 out of  37 | elapsed:   41.6s remaining:   28.4s\n",
      "[Parallel(n_jobs=10)]: Done  26 out of  37 | elapsed:   49.2s remaining:   20.8s\n",
      "[Parallel(n_jobs=10)]: Done  30 out of  37 | elapsed:   58.3s remaining:   13.6s\n",
      "[Parallel(n_jobs=10)]: Done  34 out of  37 | elapsed:  1.0min remaining:    5.3s\n",
      "[Parallel(n_jobs=10)]: Done  37 out of  37 | elapsed:  1.0min finished\n"
     ]
    }
   ],
   "source": [
    "gen_tsfresh_feature_parallel('03', 37, settings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
